package com.xbj.voa.common.aop;

import com.xbj.voa.common.shiroAndJwt.ThreadLocalToken;
import com.xbj.voa.common.utils.R;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author: 木火
 * @create: 2024-01-08
 * @description: 用于在返回时,获取ThreadLocalToken中在ShiroFilter保存的新生成的令牌字符串token,放到R对象中返回客户端
 **/
@Aspect
@Component
public class TokenAspect {
    @Resource
    private ThreadLocalToken threadLocalToken;

    @Pointcut("execution(public * com.xbj.voa.controller.*.*(..))")
    public void aspect(){

    }

    @Around("aspect()")
    public Object around(ProceedingJoinPoint point)throws Throwable{
        R r = (R)point.proceed();
        String token = threadLocalToken.getToken();
        if(token != null){
            r.put("token",token);
            threadLocalToken.clear();
        }
        return r;
    }
}
